<?php

namespace IDock\Insurance\YAIC;

use IDock\Common\BaseInterface\BuilderInterface;
use IDock\Http\Request;
use IDock\Http\Response;
use IDock\Common\Tools\XMLTools;
use IDock\Insurance\YAIC\Request\AllYAICRequest;
use IDock\Insurance\YAIC\Response\AllYAICResponse;

class Builder implements BuilderInterface
{

    public function getConfig(): array
    {
        return $this->config;
    }

    protected $config;

    protected $consumerID;

    protected $secretKey;
    protected $notifyUrl;

    public function execute(Request $request): Response
    {
        /*** @var AllYAICRequest $request */
        $request->loadConfig($this->config);

        $request->setRequestId(md5(uniqid()));

        $data = [
            'requestHead' => [
                'seqNo' => $request->getRequestId(),
                'consumerID' => $this->consumerID,
                'consumerAccessKey' => '',
                'transCode' => $request->transCode,
                'transTime' => date('Y-m-d H:i:s'),
                'regionCode' => 'Z',//四川
            ],
            'requestBody' => $this->generate($request)
        ];
        $request->setPreParameter($data);
        $request = $this->sign($request);
        return $this->request($request);
    }

    public function generate(Request $request): array
    {
        return $request->generate();
    }


    public function sign(Request $request): Request
    {
        $signStr = XMLTools::ToXML($request->generate(),'');
        $sign = base64_encode(hash_hmac('sha256',$signStr,$this->secretKey,true));
        $data = $request->getPreParameter();
        $data['requestHead']['consumerAccessKey'] = $sign;
        $xml = XMLTools::ToXML($data,'RequestData');
        $request->setBizParam($xml);
        return $request;
    }

    public function exceptionResponse($request, $errorMessage): Response
    {
        $response = $request->getResponse();

        $response = new $response;
        $response->setCode('500');
        $response->setError($errorMessage);

        return $response;
    }

    public function request(Request $request): Response
    {
        /*** @var AllYAICRequest $request */
        /*** @var AllYAICResponse $response */
        $response = $request->request($request);
        $response->setVisitor($request->getVisitor());

        $isTestResponse = $GLOBALS['testResponse'] ?? '';

        if ($response->isSuccess()) {

            if (!$this->decrypt($response)) {
                return $response;
            }

            if (!$isTestResponse) {
                $request->decryptLogs($request->getRequestId(), json_encode($response->getBody(),320));
            }

            $response->printerInit();
        }


        return $response;
    }

    /**
     * 数据加密
     * @param Request $request
     * @return bool
     */
    public function encrypt(Request $request): bool
    {
        return true;
    }

    public function decrypt(AllYAICResponse $response): bool
    {
        return true;
    }
}
